﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using SampleEventAgregator.Models;
using System.Linq;
using Windows.UI.Core;
using Windows.UI.Xaml;

namespace SampleEventAgregator.Services
{
    public class Service
    {
        public void LoadListNews()
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.kompas.com/getrss/tekno");
            request.BeginGetResponse(new AsyncCallback(ReadCallback),request);

            //var asyncrequest = (HttpWebRequest)WebRequest.Create("http://www.kompas.com/getrss/tekno");
            //asyncrequest.BeginGetResponse()
            //asyncrequest.BeginGetResponse(asyncresult =>
            //{
            //    try
            //    {

            //        var responsestream = (HttpWebResponse)asyncrequest.EndGetResponse(asyncresult);
            //        System.IO.Stream stream = responsestream.GetResponseStream();
            //        var reader = new System.IO.StreamReader(stream);
            //        string stringResult = reader.ReadToEnd();

            //        var xml = XElement.Parse(stringResult);
            //        var data = from p in xml.Descendants("item")
            //                   select new Models.ModelPost
            //                              {
            //                                  Title = (p.Element("title").Value).Trim(),
            //                                  Message = (p.Element("description").Value).Trim(),
            //                              };


            //        foreach (var p in data)
            //        {
            //            App.EventAggregator.GetEvent<SampleEvent.EventGetPost>().Publish(p);
            //        }
            //    }
            //    catch (Exception ex) { }

            //}, null);
        }

        public async void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            
            System.IO.Stream stream = response.GetResponseStream();
            var reader = new System.IO.StreamReader(stream);
            string stringResult = reader.ReadToEnd();

            var xml = XElement.Parse(stringResult);
            var data = from p in xml.Descendants("item")
                       select new Models.ModelPost
                                  {
                                      Title = (p.Element("title").Value).Trim(),
                                      Message = Regex.Replace(p.Element("description").Value, "<.+?>", string.Empty)
                                  };


            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                    foreach (var p in data)
                    {
                        App.EventAggregator.GetEvent<SampleEvent.EventGetPost>().Publish(p);
                    }
                });
        }
    }
}
